package cn.myeasyai.controller;

import cn.myeasyai.config.FileConfig;
import cn.myeasyai.dto.ImageDTO;
import cn.myeasyai.feature.FeatureExtractor;
import cn.myeasyai.service.ImageService;
import cn.myeasyai.service.UploadFileService;
import cn.myeasyai.util.IdWorker;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author fushoujiang
 */
@RequestMapping("/image")
@RestController
public class ImageController {
    @Resource
    private FeatureExtractor featureExtractor;
    @Resource
    private UploadFileService uploadFileService;
    @Resource
    private IdWorker idWorker;


    @Resource
    private ImageService imageService;;

    @Resource
    private FileConfig fileConfig;

    @PostMapping("/imageInit")
    public ImageDTO imageInit(MultipartFile file) {
        try {
            ImageDTO imageDTO = new ImageDTO();
            final long id = idWorker.nextId();
            imageDTO.setId(id);
            if (fileConfig.getIdStrategy()=="default"){
                imageDTO.setFileName(String.valueOf(id));
            }else {
                imageDTO.setFileName(file.getName());
            }
            return imageService.imageInit(file.getInputStream(),imageDTO);
        } catch (IOException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("上传失败");
    }

}
